<template>
  <el-dialog
    :title="info"
    append-to-body
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    align="left"
    v-loading="currLoading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    width="800px">
    <el-form ref="form" :model="form" label-width="150px" :rules="rules" v-if="bankList.length>0">
      <el-form-item label="账户余额" align="left">
        <span class="b red" v-text="'￥'+ToMoney(currMoney)"></span>
      </el-form-item>
      <el-form-item label="所属银行" align="left" prop="bankId">
        <el-select v-model="form.bankId" placeholder="所属银行" style="width: 300px">
          <el-option
            v-for="(item,index) in bankList"
            :key="index"
            :label="item.bank+','+item.card"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="提现金额" align="left" prop="money">
        <el-input-number :step="1" :min="100" :max="currMoney" :controls="false" class="my-input-number" v-model="form.money" style="width: 200px"></el-input-number>
      </el-form-item>
      <el-form-item label="备注" align="left" prop="info">
        <el-input v-model.trim="form.info" style="width: 200px"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'CreateShopWithdrawDialog',
  props:{
    successFunction:Function
  },
  data: function () {
    return {
      info:'新增提现',
      dialogVisible: false,
      currLoading: false,
      bankList:[],
      form: {
        bankId: '',
        money: 100,
        info: '',
      },
      currMoney:0,
      rules: {
        money: [
          {required: true, message: '请输入 提现金额', trigger: ['blur', 'change']},
        ],
        info: [
          {required: false, message: '请输入 备注信息', trigger: ['blur', 'change']},
          {min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: ['blur', 'change']}
        ],
        bankId: [
          {required: true, message: '请选择 提现银行卡', trigger: ['blur', 'change']},
        ]
      }
    }
  },
  mounted () {

  },
  methods: {
    getMoney:function (callback) {
      let $this=this
      $this.currLoading = true
      $this.MyRequest(this.ApiDoNameSupplier + 'Supplier/info', {}, function (res) {
        $this.currMoney=res.money
        callback()
        $this.$forceUpdate()
      }, function () {
        $this.$message.success('操作失败，请重试')
        $this.currLoading = false
        $this.$forceUpdate()
      })
    },
    open: function (bankList) {
      let $this=this
      $this.getMoney(function () {
        $this.bankList=bankList
        $this.form={
          money: 100,
          info: '',
          bankId: bankList[0].id,
        }
        $this.dialogVisible = true
        $this.$forceUpdate()
      })
    },
    submit: function () {
      let $this = this
      if ($this.form.money<100){
        return $this.$message.error('提现金额必须大于 100')
      }
      $this.$refs.form.validate((valid) => {
        if (valid) {
          var form = Object.assign({}, $this.form)
          $this.currLoading = true
          $this.MyRequest(this.ApiDoNameSupplier + 'SupplierWithdraw/submit', form, function () {
            $this.$message.success('操作成功,请等待管理员审核!')
            $this.dialogVisible=false
            $this.$props.successFunction()
            $this.$forceUpdate()
          }, function () {
            $this.$message.success('操作失败，请重试')
            $this.currLoading = false
            $this.$forceUpdate()
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
